package org.vaadin.vaadinjit;

import java.io.File;

import org.vaadin.vaadinjit.JitBarChart.Orientation;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.FileResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class VaadinjitApplication extends Application implements Property.ValueChangeListener {

	private static final long serialVersionUID = 1L;
	
	JitSpaceTree sTree;
	JitBarChart  barChart;
	JitAreaChart areaChart;
	JitPieChart  pieChart;
	Label title, subtitle;
	TabSheet t;
	
	@Override
	public void init() {
		ElectionData.init();
		
		Window mainWindow = new Window("ICT Showroom Demo");
		setMainWindow(mainWindow);
		VerticalLayout rootLayout = new VerticalLayout();
		//rootLayout.setSpacing(true);
		
		title = new Label("<h1>Parliament candidate support</h1>");
		title.setContentMode(Label.CONTENT_XHTML);
		subtitle = new Label("<p>Economic research by Yle</p>");
		subtitle.setContentMode(Label.CONTENT_XHTML);
		
		barChart = new JitBarChart();
		areaChart = new JitAreaChart();
		pieChart = new JitPieChart();
		
		NativeSelect pollingSelect = new NativeSelect("Select dataset");
		pollingSelect.addItem("Parliament candidate support");
		pollingSelect.addItem("Alcohol consumption");
		pollingSelect.addItem("GDP");
		pollingSelect.setNullSelectionAllowed(false);
		pollingSelect.setValue("Parliament candidate support");
		pollingSelect.setImmediate(true);
		pollingSelect.addListener(this);
		
		File img = new File("H:\\Programming\\workspace-ee\\VaadinJit\\files\\vaadin-logo.png");
		FileResource imgRsc = new FileResource(img, this);
		Embedded image = new Embedded("", imgRsc);
		
		rootLayout.addComponent(image);
		rootLayout.addComponent(title);
		rootLayout.addComponent(subtitle);
		rootLayout.addComponent(pollingSelect);
		
		t = new TabSheet();
		t.addTab(barChart, "Bar chart", null);
		t.addTab(areaChart, "Area chart", null);
		t.addTab(pieChart, "Pie chart", null);
		t.setWidth("722px");
		t.setHeight("514px");
		
		rootLayout.addComponent(t);
		mainWindow.addComponent(rootLayout);
		
		barChart.setWidth("720px");
		barChart.setHeight("480px");
		barChart.setOrientation(Orientation.VERTICAL);
		barChart.setOption("showAggregates", false);
		barChart.init();
		barChart.setJitData(ElectionData.getData("Parliament candidate support"));
		
		areaChart.setWidth("720px");
		areaChart.setHeight("480px");
		areaChart.setOption("showAggregates", false);
		areaChart.init();
		areaChart.setJitData(ElectionData.getData("Parliament candidate support"));
		
		pieChart.setWidth("720px");
		pieChart.setHeight("480px");
		pieChart.setOption("showAggregates", false);
		pieChart.init();
		pieChart.setJitData(ElectionData.getData("Parliament candidate support"));
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		if(event.getProperty().toString().equals("Parliament candidate support")) {
			barChart.setJitData(ElectionData.getData("Parliament candidate support"));
			areaChart.setJitData(ElectionData.getData("Parliament candidate support"));
			pieChart.setJitData(ElectionData.getData("Parliament candidate support"));
			title.setValue("<h1>Parliament candidate support</h1>");
			subtitle.setValue("<p>Economic research by Yle</p>");
		} else
		if(event.getProperty().toString().equals("Alcohol consumption")) {
			barChart.setJitData(ElectionData.getData("Alcohol consumption"));
			areaChart.setJitData(ElectionData.getData("Alcohol consumption"));
			pieChart.setJitData(ElectionData.getData("Alcohol consumption"));
			title.setValue("<h1>Alcohol consumption</h1>");
			subtitle.setValue("<p>Alcohol consumption in Finland: 100% alcohol, litres per capita aged 15 and over</p>");
		} else
		if(event.getProperty().toString().equals("GDP")) {
			barChart.setJitData(ElectionData.getData("GDP"));
			areaChart.setJitData(ElectionData.getData("GDP"));
			pieChart.setJitData(ElectionData.getData("GDP"));
			title.setValue("<h1>GDP</h1>");
			subtitle.setValue("<p>Real GDP in billions of US dollars</p>");
		}
	}
	
}
